package com.m3839.sdk.anti.bean;

import android.os.Parcel;
import android.os.Parcelable;

import com.m3839.sdk.anti.constant.AntiConstant;
import com.m3839.sdk.common.base.BaseBean;
import com.m3839.sdk.common.util.LogUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:PopConfigBean
 *
 * @author 陈宝阳
 * @create 2019/12/31 14: 56
 */
public class PopConfigBean extends BaseBean<PopConfigBean> implements Parcelable {

  public int type;
  public int onlyFirst;
  public int moment;
  public String title;
  public String content;
  public String tips;
  public LinkBean linkBean;
  public List<ButtonBean> buttonBeanList;
  public FinalBean finalBean;
  public LabelBean labelBean;
  public String beforeStartText;
  public String beforeStartFormat;
  public int beforeStart;

  public PopConfigBean(){}

  public PopConfigBean(JSONObject jsonObject){
    parseBean(jsonObject);
  }

  protected PopConfigBean(Parcel in) {
    type = in.readInt();
    onlyFirst = in.readInt();
    moment = in.readInt();
    title = in.readString();
    content = in.readString();
    tips = in.readString();
    linkBean = in.readParcelable(LinkBean.class.getClassLoader());
    buttonBeanList = in.createTypedArrayList(ButtonBean.CREATOR);
    finalBean = in.readParcelable(FinalBean.class.getClassLoader());
    labelBean = in.readParcelable(LabelBean.class.getClassLoader());
    beforeStartText = in.readString();
    beforeStartFormat = in.readString();
    beforeStart = in.readInt();
  }

  public static final Creator<PopConfigBean> CREATOR = new Creator<PopConfigBean>() {
    @Override
    public PopConfigBean createFromParcel(Parcel in) {
      return new PopConfigBean(in);
    }

    @Override
    public PopConfigBean[] newArray(int size) {
      return new PopConfigBean[size];
    }
  };

  @Override
  public int describeContents() {
    return 0;
  }

  @Override
  public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(type);
    dest.writeInt(onlyFirst);
    dest.writeInt(moment);
    dest.writeString(title);
    dest.writeString(content);
    dest.writeString(tips);
    dest.writeParcelable(linkBean, flags);
    dest.writeTypedList(buttonBeanList);
    dest.writeParcelable(finalBean, flags);
    dest.writeParcelable(labelBean, flags);
    dest.writeString(beforeStartText);
    dest.writeString(beforeStartFormat);
    dest.writeInt(beforeStart);
  }

  @Override
  public String toString() {
    return "PopConfigBean{" +
            "type=" + type +
            ", onlyFirst=" + onlyFirst +
            ", moment=" + moment +
            ", title='" + title + '\'' +
            ", content='" + content + '\'' +
            ", tips=" + tips +
            ", linkBean=" + linkBean +
            ", buttonBeanList=" + buttonBeanList +
            ", finalBean=" + finalBean +
            ", closeButtonBean=" + labelBean +
            ", beforeStartText='" + beforeStartText + '\'' +
            ", beforeStartFormat='" + beforeStartFormat + '\'' +
            ", beforeStart=" + beforeStart +
            '}';
  }

  @Override
  public PopConfigBean parseBean(JSONObject jsonObject) {
    if(jsonObject == null) {
      return null;
    }
    type = jsonObject.optInt(AntiConstant.KEY_TYPE);
    onlyFirst = jsonObject.optInt(AntiConstant.KEY_ONLY_FIRST);
    moment = jsonObject.optInt(AntiConstant.KEY_MOMENT);
    title = jsonObject.optString(AntiConstant.KEY_TITLE);
    content = jsonObject.optString(AntiConstant.KEY_CONTENT);
    tips = parseTips(jsonObject.optJSONArray(AntiConstant.KEY_TIPS));
    linkBean = new LinkBean(jsonObject.optJSONObject(AntiConstant.KEY_LINK));
    buttonBeanList = parseButtons(jsonObject.optJSONArray(AntiConstant.KEY_BUTTONS));
    finalBean = new FinalBean(jsonObject.optJSONObject(AntiConstant.KEY_FINAL));
    labelBean = new LabelBean(jsonObject.optJSONObject(AntiConstant.KEY_CLOSE_BUTTONS));
    beforeStart = jsonObject.optInt(AntiConstant.KEY_BEFORE_START);
    beforeStartText = jsonObject.optString(AntiConstant.KEY_BEFORE_START_TEXT);
    beforeStartFormat = jsonObject.optString(AntiConstant.KEY_BEFORE_START_FORMAT);
    LogUtils.i("m3839", toString());
    return this;
  }

  private String parseTips(JSONArray jsonArray) {
    if(jsonArray == null) {
      return null;
    }
    StringBuilder tipBuilder = new StringBuilder();
    int len = jsonArray.length();
    for (int i = 0; i < len; i++) {
      tipBuilder.append(jsonArray.optString(i));
    }
    return tipBuilder.toString();
  }


  private List<ButtonBean> parseButtons(JSONArray jsonArray) {
    if(jsonArray == null) {
      return null;
    }
    List<ButtonBean> buttonBeans = new ArrayList<>();
    int len = jsonArray.length();
    for (int i = 0; i < len; i++) {
      JSONObject buttonJson = jsonArray.optJSONObject(i);
      buttonBeans.add(new ButtonBean(buttonJson));
    }
    return buttonBeans;
  }
}
